package com.dog.basics.api.controller.mtkj;


import com.dog.basics.bean.dto.api.mtkj.ApiDepositAddDto;
import com.dog.basics.bean.vo.api.mtkj.ApiDepositVo;
import com.dog.basics.bean.vo.api.mtkj.ApiOrderPrepayVo;
import com.dog.basics.common.annotation.Limit;
import com.dog.basics.common.constant.SwaggerConstants;
import com.dog.basics.common.util.R;
import com.dog.basics.security.annotation.Inner;
import com.dog.basics.security.util.SecurityUtils;
import com.dog.basics.service.common.WeChatMiniService;
import com.dog.basics.service.mtkj.IMtDeviceDepositService;
import com.github.binarywang.wxpay.bean.notify.WxPayNotifyResponse;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import springfox.documentation.annotations.ApiIgnore;

import javax.validation.Valid;

/**
 * <p>
 * 设备押金记录 前端控制器
 * </p>
 *
 * @author wph
 * @since 2020-09-29
 */
@RestController
@RequestMapping("/api/device/deposit")
@Api(value = "设备押金记录模块", tags = SwaggerConstants.API_ALL)
@AllArgsConstructor
public class MtDeviceDepositController {
    private final IMtDeviceDepositService mtDeviceDepositService;
    private final WeChatMiniService weChatMiniService;
    /**
     * @Description: 获取我的押金列表
     * @Param: CashWithdrawalAddDto
     * @return: Boolean
     * @Author: wph
     * @Date: 2020/05/29  0027
     */
    @ApiOperation(value = "获取我的押金列表", notes = "获取我的押金列表", tags = {SwaggerConstants.API_TUIHUAN,SwaggerConstants.API_YJTH})
    @ApiImplicitParam(value = "状态:1-已支付  2-申请退款中 ", name = "depositState", dataType = "Integer", required = true)
    @GetMapping
    public R<ApiDepositVo> getApiDepositInfo(Integer depositState) {
        return R.ok(mtDeviceDepositService.getApiDepositInfo(depositState));
    }

    /**
     * @Description: 缴纳押金
     * @Param: OrderAddDto
     * @return: R
     * @Author: wph
     * @Date:
     */
    @ApiOperation(value = "缴纳押金", notes = "缴纳押金", tags = SwaggerConstants.API_SAO)
    @PostMapping("/saveObj")
    @Limit(name = "缴纳押金频繁", prefixKey = "DEPOSIT:RELEASE", period = 1, count = 1)
    public R saveObj(@Valid @RequestBody ApiDepositAddDto obj) {
        //下单
        ApiOrderPrepayVo orderPrepayVo = mtDeviceDepositService.saveObj(obj, SecurityUtils.getUser().getUserId());
        //小程序
        return weChatMiniService.callOrderPrepayment(orderPrepayVo);
    }

    /**
     * @Description: 设备押金退款
     * @Param: OrderAddDto
     * @return: R
     * @Author: wph
     * @Date:
     */
    @ApiOperation(value = "设备押金退款", notes = "设备押金退款", tags = {SwaggerConstants.API_TUIHUAN,SwaggerConstants.API_YJTH})
    @ApiImplicitParam(value = "设备押金订单ID", name = "id", dataType = "Long", required = true)
    @PutMapping("/returnObj/{id}")
    public R<Boolean> returnObj(@PathVariable("id") Long id) {
        return R.ok(mtDeviceDepositService.returnObj(id));
    }
    /**
     * 小程序微信 Order订单 - 支付回调
     *
     * @param
     * @return
     */
    @Inner
    @ApiIgnore
    @PostMapping("/wxPay/minicallbackOrder")
    public String wxMiniPayLowerOrderCallback(@RequestBody String xmlData) {
        if (mtDeviceDepositService.wxMiniPayLowerOrderCallback(xmlData)) {
            return WxPayNotifyResponse.success("成功");
        }
        return WxPayNotifyResponse.fail("失败");
    }
}
